<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

//开奖记录
class AwardrecordController extends AdminbaseController {

    protected $total_kai_jiang;

    public function _initialize() {
        parent::_initialize();

        //间隔开奖时间
        $this->jian_ge_minute = get_jian_ge_minute();
        //总开奖次数
        // $this->total_kai_jiang = intval(1440/($this->jian_ge_minute));//24*60/5
        $this->total_kai_jiang = (60 * 60 * 14) / (5 * 60);//24*60/5
    }

    /**
     *  显示列表
     */
    public function index() {
        /**搜索条件**/
        $qi_shu = I('request.qishu');
        $start_time = trim(I('request.start_time',date('Y-m-d')));

        if ($qi_shu) {
            $where['qishu'] = array('like', "%$qi_shu%");
        }

        $where['create_time'] = array('between', array($start_time . ' 00:00:00', $start_time . ' 23:59:59'));

        $M = M('award_record');

        $count = $M->where($where)->count();
        $page = $this->page($count, 25);
        $lists = $M
            ->where($where)
            ->order("create_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("lists", $lists);

        $this->display();
    }

    /**
     *  添加
     */
    public function add() {
        $this->assign('total_kai_jiang', $this->total_kai_jiang);
        $this->display();
    }

    /**
     *  提交添加
     */
    public function add_post() {
        if (IS_POST) {
            $M = M('award_record');

            $qi = intval($_POST['qishu']);

            $date_str = str_replace('-', '', $_POST['date']);

            $_POST['qishu'] = $date_str . sprintf("%03d", $qi);

            //寻找是否有同期的
            $res = $M->where(array('qishu='.$_POST['qishu']))->getField('id');
            if($res){
                $this->error('已存在 ' . $_POST['qishu'] . ' 的开奖记录,请删除 ' . $_POST['qishu'] . ' 的开奖记录,再添加!');
            }

            $minute = ($qi - 1) * ($this->jian_ge_minute);
            $_POST['create_time'] = date('Y-m-d H:i:s', strtotime($_POST['date'] . ' 00:00:00' . ' + ' . $minute . ' minute'));
            $_POST['update_time'] = date('Y-m-d H:i:s');

            unset($_POST['date']);

            $res = $M->add($_POST);
            if ($res === false) {
                $this->error('添加失败!');
            }

            $this->success('添加成功!', U('index'));
        }
    }

    /**
     *  编辑
     */
    public function edit() {
        $where['id'] = I('get.id');
        $list = M('award_record')
            ->where($where)
            ->find();
        $this->assign($list);
        $this->assign('total_kai_jiang', $this->total_kai_jiang);
        $this->display();
    }

    /**
     *  提交编辑
     */
    public function edit_post() {
        if (IS_POST) {

            $_POST['update_time'] = date('Y-m-d H:i:s');

            $res = M('award_record')
                ->save($_POST);

            if ($res === false) {
                $this->error('编辑失败!');
            }

            $this->success('编辑成功!', U('index'));
        }
    }


    /**
     *  删除
     */
    public function delete() {
        $M = M('award_record');
        if (isset($_POST['ids'])) {
            $ids = implode(",", $_POST['ids']);
            if ($M->where("id in ($ids)")->delete()) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        } else {
            if (isset($_GET['id'])) {
                $id = intval($_GET['id']);
                if ($M->where('id=' . $id)->delete()) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }
        }
    }

    public function automake() {
        echo $this->total_kai_jiang;
        $this->display();
    }

    //提交自动生成
    public function automake_post() {
        $date = I('post.date', date('Y-m-d'));

        $M = M('award_record');
        $where['create_time'] = array('between', array($date . ' 10:00:00', $date . ' 23:59:59'));
        $find_res = $M->where($where)->find();
        if ($find_res) {
            $this->error("已存在 $date 的数据! 请重新输入日期,或先删除 $date 数据");
        }

        //定义需添加的数据
        $add_data = array();

        for ($i = 1; $i <= $this->total_kai_jiang; $i++) {

            for ($j = 1; $j <= 6; $j++) {
                $temp_num = mt_rand(1, 12);
                $add_data['num_' . $j] = $temp_num;
            }

            $date_str = str_replace('-', '', $date);
            $add_data['qishu'] = $date_str . sprintf("%03d", $i);

            $minute = ($i - 1) * $this->jian_ge_minute;
            $add_data['create_time'] = date('Y-m-d H:i:s', strtotime($date . ' 10:00:00' . ' + ' . $minute . ' minute'));
            $add_data['update_time'] = date('Y-m-d H:i:s');

            $res = $M->add($add_data);
            unset($add_data);
        }

        $this->success("自动生成成功! ", U('index', array('start_time' => $date)));
    }

}